package com.puzzlebazar.server;

import javax.jdo.Extent;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.puzzlebazar.client.admin.SiteInfo;
import com.puzzlebazar.client.services.SettingsService;
import com.puzzlebazar.client.util.Capabilities;

public class SettingsServiceImpl extends MainServiceServlet implements
	SettingsService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7955732628623638420L;

	@Override
	public boolean resetDatabase() {
		
		UserService userService = UserServiceFactory.getUserService();
		if( !userService.isUserLoggedIn() || !userService.isUserAdmin() || !Capabilities.canResetDatabase(getCurrentUserInfo()) )
			return false;

	    final PersistenceManager pm = PMF.get().getPersistenceManager();

	    Extent<PuzzleInfoJdo> allPuzzleInfoJdo = pm.getExtent(PuzzleInfoJdo.class, false);
	    for( PuzzleInfoJdo jdo : allPuzzleInfoJdo ) 
	    	pm.deletePersistent(jdo);
        allPuzzleInfoJdo.closeAll();	    

	    Extent<UserInfoJdo> allUserInfoJdo = pm.getExtent(UserInfoJdo.class, false);
	    for( UserInfoJdo jdo : allUserInfoJdo ) 
	    	pm.deletePersistent(jdo);
        allUserInfoJdo.closeAll();	    

	    Extent<SiteInfoJdo> allSiteInfoJdo = pm.getExtent(SiteInfoJdo.class, false);
	    for( SiteInfoJdo jdo : allSiteInfoJdo ) 
	    	pm.deletePersistent(jdo);
        allSiteInfoJdo.closeAll();	    
	    
		return true;
	}

	@Override
	public SiteInfo loadSiteInfo() {

	    final PersistenceManager pm = PMF.get().getPersistenceManager();
	    SiteInfo result = new SiteInfo();
		SiteInfoJdo jdo = null;
	    try {
	    	jdo = pm.getObjectById(SiteInfoJdo.class, SiteInfoJdo.UniqueId);
	    	jdo.to( result );
	    } catch( JDOObjectNotFoundException exception ) {
	    	modifySiteInfo( result );
	    }	    
	    finally {
	        pm.close();
	    }
	    
	    // TODO Not hardcode?
    	HttpServletRequest request = this.getThreadLocalRequest();
	    String thisURL = "http://" + request.getServerName() + ":" + request.getServerPort(); 
	    	
	    UserService userService = UserServiceFactory.getUserService();	    
	    result.setLoginURL( userService.createLoginURL(thisURL) );
	    result.setLogoutURL( userService.createLogoutURL(thisURL) );
	    
	    return result;

	}

	@Override
	public boolean modifySiteInfo(SiteInfo siteInfo) {

		UserService userService = UserServiceFactory.getUserService();
		if( !userService.isUserLoggedIn() || !userService.isUserAdmin() || 
				!Capabilities.canModifySiteInfo(getCurrentUserInfo()) )
			return false;
		
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		SiteInfoJdo jdo = new SiteInfoJdo();
		jdo.from( siteInfo );
		try {
			pm.makePersistent( jdo );
		}
		finally {
			pm.close();
		}
		
		return true;		
	}

}
